import { request } from '../../utils/request'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    src: '',
    painting: {},
    shareImage: '',
    height: 250,
    diffHeight: 100,
    imgHeight: 250,
    from: '',
    qrCode: '',
    windowWidth: '',
    windowHeight: 834
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      user: JSON.parse(options.user)
    })
    this.getQrCode()
    this.setData({
      windowWidth: wx.getSystemInfoSync()
        .windowWidth
    })
  },

  getQrCode() {
    request({
      url: '/mini/api/mine/getQrCode',
      method: 'GET'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          qrCode: resdata.data
        })
        setTimeout(() => {
          this.eventDraw()
        }, 1000)
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  eventDraw() {
    wx.showLoading({
      title: '绘制分享图片中',
      mask: true
    })
    this.setData({
      painting: {
        width: wx.getSystemInfoSync().windowWidth,
        height: this.data.height,
        clear: true,
        views: [
          {
            type: 'image',
            url: '../../img/blackBg.jpg',
            top: 0,
            left: 0,
            width: this.data.windowWidth,
            height: this.data.height
          },
          {
            type: 'image',
            url: '../../img/share.png',
            top: 0,
            left: 0,
            width: this.data.windowWidth,
            height: this.data.imgHeight
          },
          {
            type: 'text',
            content: this.data.user.nickName,
            fontSize:
              0.0143 * this.data.windowHeight,
            color: '#333333',
            textAlign: 'left',
            top: 0.1882 * this.data.windowHeight,
            left: 0.1976 * this.data.windowWidth,
            bolder: true
          },
          {
            type: 'text',
            content: this.data.user.address.substring(
              0,
              12
            ),
            fontSize:
              0.0123 * this.data.windowHeight,
            color: '#333333',
            fontFamily: 'Microsoft YaHei',
            textAlign: 'left',
            top: 0.2165 * this.data.windowHeight,
            left: 0.1976 * this.data.windowWidth,
            bolder: true
          },
          {
            type: 'image',
            url: this.data.qrCode,
            top: 0.1518 * this.data.windowHeight,
            left: 0.3353 * this.data.windowWidth,
            width: 50,
            height: 50
          },
          {
            type: 'image',
            url: this.data.user
              .groupLeaderShowPic,
            top: 0.0539 * this.data.windowHeight,
            left: 0.5953 * this.data.windowWidth,
            width: 123,
            height: 160
          }
        ]
      }
    })
  },

  eventSave() {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },

  eventGetImage(event) {
    wx.hideLoading()
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
